﻿
using System.IO;
using System.Web;
using System.Web.Mvc;
using Dexter.Logger.Contracts;
using Dexter.Services;
using Dexter.Web.Mvc.Extensions;
using Dexter.Web.Mvc.Filters;
using Dexter.Web.Mvc.ViewModel.ViewsData;
using Dexter.Web.Mvc.ViewsData;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.Controllers.Backoffice {
	[Authorize ( Roles = "Administrator" )]
	[DexterCallFilter ( false )]
	public class BackupController : BackOfficeControllerBase {
		readonly ICommentService commentService;
		readonly ICommentExport commentExport;
		readonly IImportService importService;

		public BackupController ( IConfigurationService configurationService , IWebUtils webUtils , ILogger logger , IUrlBuilder urlBuilder , IContactService contactService, ICommentExport commentExport, ICommentService commentService, IImportService importService) : base ( configurationService , webUtils , logger , urlBuilder , contactService ) {
			this.commentExport = commentExport;
			this.commentService = commentService;
			this.importService = importService;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Comments() {
			return View ( base.GetViewData ( new BackOfficeBaseViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) ) );
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Disqus()
		{
			var comments = commentService.GetRecent(100000);
			
			commentExport.GenerateDisqusXml(comments);

			return this.ReturnFeedback(MessageType.Positive, "CommentExportStarted", true);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult ImportPosts() {
			return View(base.GetViewData(new BackOfficeBaseViewData(base.WebUtils, base.Configuration, base.UrlBuilder)));
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult ImportPosts(HttpPostedFileWrapper file) {
			if (file == null)
			{
				return this.ReturnFeedback(MessageType.Negative, "ImportFileFailed", base.UrlBuilder.BackOffice.ImportPosts);
			}

			importService.ImportFromBlogMl(file.InputStream.GetAllBytes());

			return this.ReturnFeedback(MessageType.Positive, "ImportComplete", base.UrlBuilder.BackOffice.Home);
		}
	}
}
